﻿// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// This file is part of CodingEditor.
// Note:	This project is derived from Peter Project
//			(hosted on sourceforge and codeplex)
//
// Copyright (c) 2008-2009, CE Team
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


namespace CE.Main
{
	using System.Globalization;

	static class HexUtils
	{
		#region Methods

		public static string GetDisplayBytes(long size)
		{
			const long multi = 1024;
			long kb = multi;
			long mb = kb * multi;
			long gb = mb * multi;
			long tb = gb * multi;

			const string bytes = "Bytes";
			const string kb1 = "KB";
			const string mb1 = "MB";
			const string gb1 = "GB";
			const string tb1 = "TB";

			string result;
			if (size < kb)
				result = string.Format("{0} {1}", size, bytes);
			else if(size < mb)
				result = string.Format("{0} {1} ({2} Bytes)",
				                       ConvertToOneDigit(size, kb), kb1, ConvertBytesDisplay(size));
			else if(size < gb)
				result = string.Format("{0} {1} ({2} Bytes)",
				                       ConvertToOneDigit(size, mb), mb1, ConvertBytesDisplay(size));
			else if(size < tb)
				result = string.Format("{0} {1} ({2} Bytes)",
				                       ConvertToOneDigit(size, gb), gb1, ConvertBytesDisplay(size));
			else
				result = string.Format("{0} {1} ({2} Bytes)",
				                       ConvertToOneDigit(size, tb), tb1, ConvertBytesDisplay(size));

			return result;
		}

		static string ConvertBytesDisplay(long size)
		{
			return size.ToString("###,###,###,###,###", CultureInfo.CurrentCulture);
		}

		static string ConvertToOneDigit(long size, long quan)
		{
			double result = (double)size / (double)quan;
			string sResult = result.ToString("0.#", CultureInfo.CurrentCulture);
			return sResult;
		}

		#endregion Methods
	}
}